package com.example.lsq.service.serviceImpl;

import com.example.lsq.dao.Userdao;

import com.example.lsq.entity.User;
import com.example.lsq.service.UserService;
import com.example.lsq.util.Result;
import com.example.lsq.util.Resultsubmit;
import com.example.lsq.util.TokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private Userdao userDao;

    @Override
    public Result login(String userId, String password, HttpServletRequest request) {
        User user =userDao.login(userId);
        if(user==null){
            return Resultsubmit.error("登录失败，密码不存在");
        }
        if(password.equals(user.getPassword())){
            HttpSession session = request.getSession();
            session.setAttribute("userId",user.getUserId());
            session.setAttribute("userName",user.getUserName());
            session.setAttribute("userRoleName",user.getRoleName());
            long time = System.currentTimeMillis();
            session.setAttribute("Time",time);
            session.setAttribute("token", TokenUtil.grtToken(
//                    String.valueOf(user.getUserId(),user.getRoleName(),time));
                String.valueOf(user.getUserId()),user.getRoleName(),time));


            return Resultsubmit.success(sweet(user));

        }else {
            return Resultsubmit.error("登录失败，密码错误");
        }





    }


    private User sweet(User user){
        User sweet = new User();
        sweet.setUserId(user.getUserId());
        sweet.setUserName(user.getUserName());
        sweet.setRoleId(user.getRoleId());
        return sweet;
    }

}


